package 恰好型滑动窗口

func numSubarraysWithSum(nums []int, goal int) int {
	s1 := 0
	s2 := 0
	left1 := 0
	left2 := 0
	cnt1, cnt2 := 0, 0
	//越长越合法 一般要写 ans += left。
	for right, num := range nums {
		s1 += num
		s2 += num
		for s1 >= goal && left1 <= right {
			s1 -= nums[left1]
			left1++

		}
		cnt1 += left1
		for s2 >= goal+1 && left2 <= right {
			s2 -= nums[left2]
			left2++
		}
		cnt2 += left2
	}
	return cnt1 - cnt2
}
